import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import Login from '@/components/Login'
import Register from '@/components/Register'
import RestPassword from '@/components/RestPassword'
import About from '@/components/About'
import CourseCenter from '@/components/CourseCenter'
import Coding from '@/components/Coding'

import UserRouter from './user'
// import store from '@/store'
// import { getUserInfo } from '@/utils/api'

Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/',
      name: 'HelloWorld',
      component: HelloWorld
    },
    {
      path: '/login',
      name: 'login',
      component: Login
    },
    {
      path: '/register',
      name: 'register',
      component: Register
    },
    {
      path: '/restpwd',
      name: 'restpwd',
      component: RestPassword
    },
    {
      path: '/about',
      name: 'about',
      component: About
    },
    {
      path: '/courses',
      name: 'courses',
      component: CourseCenter
    },
    {
      path: '/coding',
      name: 'coding',
      component: Coding
    }, {
      path: '/coding/:questionId/:questionType/:text',
      name: 'homeworkCoding',
      component: Coding,
      props: true
    }, {
      path: '/coding/:questionId/:questionType/:projectId/:text/:courseId',
      name: 'homework',
      component: Coding,
      props: true
    },
    UserRouter
  ]
})

// const whiteList = [
//   '/', '/login', 'register', '/resetpwd', '/about', '/courses'
// ]
// router.beforeEach((to, from, next) => {
//   if (whiteList.indexOf(to.path) !== -1) {
//     next()
//   } else if (localStorage.getItem('userId')) {
//     if (store.state.userInfo !== null) {
//       next()
//       return
//     }
//     let userId = localStorage.getItem('userId')
//     getUserInfo(userId).then(res => {
//       store.commit('setUserInfo', res.result)
//       next()
//     })
//   } else {
//     next('/login')
//   }
// })

export default router
